// 交叉类型 将多个类型合并为一个类型 可以理解为多个类合并为一个类 并集
function interleaving<T, U>(first: T, second: U): T & U {
  let result = <T & U>{}
  // 先拷贝第一个对象的属性
  for (const key in first) {
    (<any>result)[key] = (<any>first)[key]
  }
  // 拷贝第二个对象的属性
  for (const key in second) {
    if (!(<any>result).hasOwnProperty(key)) {
      (<any>result)[key] = (<any>second)[key]
    }

  }
  return result
}

// 测试
class Person {
  public name: string

  public constructor(name: string) {
    this.name = name
  }
}

interface Loggable {
  log(): void
}

class Loggabler implements Loggable {
  public log(): void {
    console.log('logging')
  }
}
// 把两个类组合成一个类
let jim = interleaving(new Person('jim'), new Loggabler())
console.log(jim.name)
jim.log()
